import React, { Component, Fragment } from "react";
// antd
import { Table, Button, Col, Pagination, Form, Input } from "antd";
// http
import http from '../../../utils/request';


class CloudAlarmSms extends Component {
    constructor(props){
        super(props);
        this.state = {
            // 表格分页
            total: '',
            page: 1,
            page_number: 10,
            dataSource: [],
            columns: [
                {title: "姓名", dataIndex: "name", key: "name"},
                {title: "手机", dataIndex: "mobile", key: "mobile"},
                {title: "时间", dataIndex: "created", key: "created"},
                {title: "告警级别", dataIndex: "level", key: "level"},
                {title: "告警内容", dataIndex: "content", key: "content"},
                {title: "短信状态", dataIndex: "sms_code", key: "sms_code"},
                {title: "短信内容", dataIndex: "sms_text", key: "sms_text"},
            ],
            loading: false,
            search_name: null,
        }
    }

    componentDidMount() {
        this.loadData()
    }

    loadData = () => {
        this.setState({
            loading: true
        })
        const requestData = {
            'page': this.state.page,
            'limit': this.state.page_number,
            'name__contains': this.state.search_name,
        }

        // 获取分组列表
        http.get('/alarm/sms', {params: requestData}).then(response=>{
            this.setState({
                dataSource: response.data.data.items,
                total: response.data.data.pages.total,
                loading: false
            })
        }).catch()
    }

    /** 切换每页数量 */
    onChangeSizePage = (page, page_number) => {
        this.setState({
            page: page,
            page_number: page_number,
        }, () => {
            this.loadData()
        })
    }

    /** 云平台搜索 */
    onFinishSearch = (data) => {
        this.setState({
            page: 1,
            search_name: data.name ? data.name: null,
        })
        this.loadData()
    }

    render(){
        const { loading, dataSource, columns, page, total } = this.state
        const rowSelection = {
            onChange: this.onCheckbox
        }
        var myStyle = {margin: '20px'}
        var myStyle1 = {margin: '10px', float: 'right'}
        return (
            <Fragment>
                <Form layout="inline" onFinish={this.onFinishSearch}>
                    <Form.Item label="名称" name="name">
                        <Input placeholder="请输入名称"  />
                    </Form.Item>

                    <Form.Item shouldUpdate={true}>
                        <Button type="primary" htmlType="submit">搜索</Button>
                    </Form.Item>
                </Form>
                
                <div style={myStyle1}><Button type="primary" onClick={this.onAddCloudPlatformBtn}></Button></div>
                <Table
                    pagination={false} 
                    rowSelection={rowSelection} 
                    rowKey="id" 
                    columns={columns} 
                    dataSource={dataSource}
                    loading={loading}
                    scroll={{ y: 500, x: '70vw' }}
                >
                </Table>

                <div style={myStyle}>
                    <Col span={16}>
                        <Pagination
                            onChange={this.onChangeSizePage}
                            current={page}
                            total={total}
                            showSizeChanger
                            showQuickJumper
                            showTotal={total => `Total ${total} items`}
                        />
                    </Col>
                </div>

            </Fragment>
        )
    }
}
export default CloudAlarmSms;